package leetcode.doublepointer;

//11. 盛最多水的容器
public class MaxArea {

    public static void main(String[] args) {

    }

    //双指针
    public int maxArea(int[] height) {
        int p = 0;
        int q = height.length - 1;
        int res = 0;
        while (p < q) {
            int area = (q - p) * Math.min(height[p], height[q]);//面积
            res = Math.max(area, res);
            if (height[p] <= height[q]) {
                p++;
            } else {
                q--;
            }
        }
        return res;
    }

    //暴力----超时
    public int maxArea2(int[] height) {
        int res = 0;
        for (int i = 0; i < height.length; i++) {
            for (int j = i + 1; j < height.length; j++) {
                res = Math.max((j - i) * Math.min(height[i], height[j]), res);
            }
        }
        return res;
    }


    //双指针
    public int maxAreaP(int[] height) {
        int res = 0;
        int p = 0;
        int q = height.length - 1;
        while (p < q) {
            //面积
            res = Math.max((q - p) * Math.min(height[p], height[q]), res);
            if (height[p] <= height[q]) {
                p++;
            } else {
                q--;
            }
        }

        return res;

    }
}
